﻿using Stee.CAP8.MessageBus.Sagas.Core.Extensions;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace Stee.CAP8.MessageBus.Sagas.Core.Serializaion
{
    class SimpleAssemblyQualifiedMessageTypeNameConvention : IMessageTypeNameConvention
    {
        readonly ConcurrentDictionary<Type, string> _typeToName = new ConcurrentDictionary<Type, string>();
        readonly ConcurrentDictionary<string, Type> _nameToType = new ConcurrentDictionary<string, Type>();

        public string GetTypeName(Type type) => _typeToName.GetOrAdd(type, _ => type.GetSimpleAssemblyQualifiedName());

        public Type GetType(string name) => _nameToType.GetOrAdd(name, _ => Type.GetType(name));
    }

    public interface IMessageTypeNameConvention
    {
        /// <summary>
        /// Responsible for getting an appropriate name for the given <paramref name="type"/>
        /// </summary>
        string GetTypeName(Type type);

        /// <summary>
        /// Responsible for getting the type that corresponds to the given <paramref name="name"/>
        /// </summary>
        Type GetType(string name);
    }
}
